//
//  StartView.m
//  Drops
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "StartView.h"
#define Screen_Bounds [UIScreen mainScreen].bounds
#define sKeyWindow [UIApplication sharedApplication].keyWindow
@interface StartView ()
@property(nonatomic, strong)UIImageView *bgImageView;
@property(nonatomic, strong)UILabel *descLabel;
@end

@implementation StartView

+ (instancetype)startView{
    return [[self alloc]initWithBgImage:[UIImage imageNamed:@"start_bg_02.png"] descriptionStr:nil];
}

- (instancetype)initWithBgImage:(UIImage *)bgImage descriptionStr:(NSString *)description{
    if (self == [super initWithFrame:Screen_Bounds]) {
        self.backgroundColor = [UIColor blackColor];
        //添加图片
        self.bgImageView = [[UIImageView alloc]initWithFrame:Screen_Bounds];
        self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:self.bgImageView];
        //添加描述
        self.descLabel = [[UILabel alloc]initWithFrame:CGRectMake(sWIDTH / 2 - 80, sHEIGHT - 70 , 160, 40)];
        self.descLabel.font = [UIFont systemFontOfSize:15];
        self.descLabel.textAlignment = NSTextAlignmentCenter;
        self.descLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.descLabel];
        [self configBgImage:bgImage desc:description];
        
    }
    return self;
}
- (void)configBgImage:(UIImage *)bgImage desc:(NSString *)desc{
    self.bgImageView.image = bgImage;
    self.descLabel.text = desc;
    //更新视图层次的约束从下到上的接收器。如果ui窗口的实现创建了一个布局引擎
    [self updateConstraintsIfNeeded];
}

- (void)startAnimationWithCompletionBlock:(void (^)(StartView *))completionBlock{
    [sKeyWindow addSubview:self];
    [sKeyWindow bringSubviewToFront:self];
    self.bgImageView.alpha = 0.0;
    self.descLabel.alpha = 0.0;
    [UIView animateWithDuration:3.0 animations:^{
        self.bgImageView.alpha = 1.0;
        self.descLabel.alpha = 1.0;
    } completion:^(BOOL finished) {
        //执行移走的动画
        [UIView animateWithDuration:0.6 animations:^{
            //改变frame
            CGRect frame = self.frame;
            frame.origin.x = -sWIDTH;
            self.frame = frame;
        } completion:^(BOOL finished) {
            //移除
            [self removeFromSuperview];
            if (completionBlock) {
                completionBlock(self);
            }
        }];
    }];

}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
